const { mySetInterval, myClearInterval } = require("./template.js")

function testSetTimeoutInterval() {
  console.log("=== setTimeout模拟setInterval测试 ===")

  console.log(
    "1. 基本定时功能:",
    (() => {
      let count = 0
      const timer = mySetInterval(() => {
        count++
      }, 50)

      // 等待一段时间后检查
      return new Promise((resolve) => {
        setTimeout(() => {
          myClearInterval(timer)
          resolve(count >= 3) // 50ms内应该至少执行3次
        }, 200)
      })
    })()
  )

  console.log(
    "2. 清除定时器功能:",
    (() => {
      let count = 0
      const timer = mySetInterval(() => {
        count++
      }, 30)

      // 立即清除
      myClearInterval(timer)

      return new Promise((resolve) => {
        setTimeout(() => {
          resolve(count === 0) // 清除后不应该执行
        }, 100)
      })
    })()
  )

  console.log(
    "3. 参数传递:",
    (() => {
      let receivedArgs = null
      const timer = mySetInterval((a, b) => {
        receivedArgs = [a, b]
      }, 50)

      return new Promise((resolve) => {
        setTimeout(() => {
          myClearInterval(timer)
          resolve(
            receivedArgs &&
              receivedArgs[0] === "hello" &&
              receivedArgs[1] === "world"
          )
        }, 100)
      })
    })()
  )

  console.log(
    "4. this绑定:",
    (() => {
      const obj = {
        value: 0,
        increment: function () {
          this.value++
        }
      }

      const timer = mySetInterval(obj.increment.bind(obj), 50)

      return new Promise((resolve) => {
        setTimeout(() => {
          myClearInterval(timer)
          resolve(obj.value >= 2)
        }, 150)
      })
    })()
  )

  console.log(
    "5. 多个定时器:",
    (() => {
      let count1 = 0
      let count2 = 0

      const timer1 = mySetInterval(() => count1++, 40)
      const timer2 = mySetInterval(() => count2++, 60)

      return new Promise((resolve) => {
        setTimeout(() => {
          myClearInterval(timer1)
          myClearInterval(timer2)
          resolve(count1 >= 2 && count2 >= 1)
        }, 120)
      })
    })()
  )

  console.log(
    "6. 错误处理:",
    (() => {
      let errorCaught = false
      const timer = mySetInterval(() => {
        throw new Error("test error")
      }, 50)

      // 捕获错误
      process.on("uncaughtException", () => {
        errorCaught = true
        myClearInterval(timer)
      })

      return new Promise((resolve) => {
        setTimeout(() => {
          resolve(errorCaught)
        }, 100)
      })
    })()
  )

  console.log(
    "7. 返回值类型:",
    (() => {
      const timer = mySetInterval(() => {}, 100)
      const result = typeof timer
      myClearInterval(timer)
      return result === "object" || result === "number"
    })()
  )

  console.log(
    "8. 重复清除:",
    (() => {
      let count = 0
      const timer = mySetInterval(() => {
        count++
      }, 50)

      myClearInterval(timer)
      myClearInterval(timer) // 重复清除不应该报错

      return new Promise((resolve) => {
        setTimeout(() => {
          resolve(count === 0)
        }, 100)
      })
    })()
  )
}

testSetTimeoutInterval()
